package com.report.task;

import com.aliyun.oss.model.OSSObject;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/12 21:55
 */
public class DownloadTask {

    private OSSObject ossObject;

    private HttpServletResponse response;

    public DownloadTask(OSSObject ossObject, HttpServletResponse response) {
        this.ossObject = ossObject;
        this.response = response;
    }

    public   void run(){
         try {
             long fileSize = ossObject.getObjectMetadata().getContentLength();

             InputStream objectContent = ossObject.getObjectContent();
             response.setCharacterEncoding("utf-8");
             response.setHeader("Access-Control-Allow-Origin", "*");
             response.setContentType("application/pdf");
             response.setHeader("Content-Length", String.valueOf(fileSize));

             BufferedInputStream input = new BufferedInputStream(objectContent);
             byte[] buffBytes = new byte[1024];
              BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
             int len = 0;
             while ((len = input.read(buffBytes)) != -1) {
                 bufferedOutputStream.write(buffBytes, 0, len);
             }

             bufferedOutputStream.flush();
             bufferedOutputStream.close();
            input.close();
             objectContent.close();
            ossObject.close();
        }catch (Exception e){

        }
    }
}
